package com.briup.web;

import com.briup.pojo.Customer;
import com.briup.service.CustomerService;
import com.briup.serviceImp.CustomerServiceImp;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


public class CustomerLoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        CustomerService customerServiceImp = new CustomerServiceImp();
        Customer customer = customerServiceImp.login(username);
        if (customer == null){
            request.setAttribute("user","用户名或密码错误");
            request.getRequestDispatcher("/login.jsp").forward(request,response);
        }else if (customer.getPassword().equals(password)){
            request.getSession().setAttribute("login",customer);
            request.getRequestDispatcher("/index.jsp").forward(request,response);
        }else {
            request.setAttribute("user","用户名或密码错误");
            request.getRequestDispatcher("/login.jsp").forward(request,response);

        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
